﻿using UnityEngine;
using System.Collections;
using PureMVC.Patterns;
using PureMVC.Interfaces;
public class UIBaseBehavior <T>: MonoBehaviour where T : Mediator, IMediator, new() {

	private T mMedtator;

	private void SetMediator(T mediator) {
		mMedtator = mediator;
	}

	public T GetMediator() {
		if (mMedtator == null) {
			CreateUI();
		}
		return mMedtator;
	}

	//public void CreateUI<U>() where U : UIBaseBehavior<T> {
	//	T mediator = new T();
	//	//if (mediatorName != null)
	//	//	mediator.MediatorName = mediatorName;

	//	SetMediator(mediator);
	//	mediator.ViewComponent = this;
	//}

	private void CreateUI () {
		T mediator = new T();
		SetMediator(mediator);
		mediator.ViewComponent = this;
	}
	
	public void OnRegisterMediator(IFacade facade) {
		facade.RegisterMediator(GetMediator());
	}

	public void UnRegisterMediator(IFacade facade) {
		facade.RemoveMediator(GetMediator().MediatorName);
	}
}
